import React from 'react';
import { BrowserRouter as Router, Routes, Route, Navigate } from 'react-router-dom';
import { ConfigProvider, theme } from 'antd';
import zhCN from 'antd/locale/zh_CN';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import dayjs from 'dayjs';
import 'dayjs/locale/zh-cn';

import { useAuthStore } from '@store/auth';
import { useThemeStore } from '@store/theme';
import Layout from '@components/Layout';
import Login from '@pages/auth/Login';
import Dashboard from '@pages/Dashboard';
import AgentList from '@pages/agents/AgentList';
import AgentDetail from '@pages/agents/AgentDetail';
import AgentCreate from '@pages/agents/AgentCreate';
import ChatPage from '@pages/chat/ChatPage';
import ToolsPage from '@pages/tools/ToolsPage';
import SettingsPage from '@pages/settings/SettingsPage';
import NotFound from '@pages/NotFound';

import '@/styles/global.scss';

// 设置 dayjs 中文
dayjs.locale('zh-cn');

// 创建 React Query 客户端
const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      staleTime: 5 * 60 * 1000, // 5分钟
      cacheTime: 10 * 60 * 1000, // 10分钟
      retry: 2,
      refetchOnWindowFocus: false,
    },
    mutations: {
      retry: 1,
    },
  },
});

const App: React.FC = () => {
  const { isAuthenticated } = useAuthStore();
  const { isDarkMode } = useThemeStore();

  return (
    <QueryClientProvider client={queryClient}>
      <ConfigProvider
        locale={zhCN}
        theme={{
          algorithm: isDarkMode ? theme.darkAlgorithm : theme.defaultAlgorithm,
          token: {
            colorPrimary: '#1890ff',
            colorInfo: '#1890ff',
            colorSuccess: '#52c41a',
            colorWarning: '#faad14',
            colorError: '#ff4d4f',
            borderRadius: 6,
            wireframe: false,
          },
          components: {
            Layout: {
              headerBg: isDarkMode ? '#001529' : '#ffffff',
              siderBg: isDarkMode ? '#001529' : '#ffffff',
            },
            Menu: {
              darkItemBg: '#001529',
              darkSubMenuItemBg: '#000c17',
            },
          },
        }}
      >
        <Router>
          <div className="app">
            <Routes>
              {/* 认证路由 */}
              <Route 
                path="/login" 
                element={
                  isAuthenticated ? (
                    <Navigate to="/dashboard" replace />
                  ) : (
                    <Login />
                  )
                } 
              />
              
              {/* 受保护的路由 */}
              <Route 
                path="/*" 
                element={
                  isAuthenticated ? (
                    <Layout>
                      <Routes>
                        <Route path="/" element={<Navigate to="/dashboard" replace />} />
                        <Route path="/dashboard" element={<Dashboard />} />
                        
                        {/* 智能体相关路由 */}
                        <Route path="/agents" element={<AgentList />} />
                        <Route path="/agents/create" element={<AgentCreate />} />
                        <Route path="/agents/:id" element={<AgentDetail />} />
                        
                        {/* 对话页面 */}
                        <Route path="/chat/:agentId?" element={<ChatPage />} />
                        
                        {/* 工具页面 */}
                        <Route path="/tools" element={<ToolsPage />} />
                        
                        {/* 设置页面 */}
                        <Route path="/settings/*" element={<SettingsPage />} />
                        
                        {/* 404 页面 */}
                        <Route path="*" element={<NotFound />} />
                      </Routes>
                    </Layout>
                  ) : (
                    <Navigate to="/login" replace />
                  )
                } 
              />
            </Routes>
          </div>
        </Router>
      </ConfigProvider>
    </QueryClientProvider>
  );
};

export default App;